
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Java中Service与Mapper的关系详解 - 学习卡片</title>
      <style>
        body { font-family: sans-serif; background-color: #f0f8ff; color: #333; display: flex; flex-direction: column; align-items: center; padding: 50px 20px; }
        .header h1 { font-size: 32px; }
        .grid-container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 28px; width: 100%; max-width: 1200px; }
        .card-container { perspective: 1200px; cursor: pointer; height: 250px; }
        .card { width: 100%; height: 100%; position: relative; transform-style: preserve-3d; transition: transform 0.7s; border-radius: 16px; box-shadow: 0 4px 16px rgba(0,0,0,0.08); }
        .card-container.flipped .card { transform: rotateY(180deg); }
        .card-face { position: absolute; width: 100%; height: 100%; backface-visibility: hidden; display: flex; flex-direction: column; box-sizing: border-box; border-radius: 16px; background-color: #fff; padding: 24px; }
        .card-back { background-color: #f0fff4; transform: rotateY(180deg); justify-content: space-between; }
        .card-category { font-size: 14px; color: #0052d9; margin-bottom: 8px; font-weight: 500; }
        .card-question { font-size: 20px; font-weight: 500; flex-grow: 1; display: flex; align-items: center; justify-content: center; text-align: center; }
        .card-answer-wrapper { flex-grow: 1; overflow-y: auto; }
        .card-answer { font-size: 15px; line-height: 1.7; }
        .card-footer { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 16px; margin-top: 16px; }
        .card-source { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 12px; margin-top: 12px; }
      </style>
    </head>
    <body>
      <div class="header">
        <h1>Java中Service与Mapper的关系详解 - 学习卡片</h1>
      </div>
      <div class="grid-container">
        
    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">理论</div>
          <div class="card-question">在文档的比喻中，Java三层架构的Service、Mapper和Controller层分别对应餐厅里的哪三个角色？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">理论</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">Service层对应服务员，负责接待顾客、记录需求、协调后厨；Mapper层对应厨师，专注烹饪、处理食材、制作菜品；Controller层对应传菜员，负责传递菜品、响应顾客需求。</div>
          </div>
          <div class="card-source">来源: 一、开篇故事：餐厅里的"代码架构"</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">理论</div>
          <div class="card-question">根据文档，代码分层架构主要带来了哪三大好处？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">理论</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">分层架构带来的三大好处是：1. 解耦合（例如修改数据库不影响业务逻辑）；2. 可维护性（各司其职，方便排查问题）；3. 复用性（例如同一Service可调用多个Mapper）。</div>
          </div>
          <div class="card-source">来源: 2.2 为什么需要分层？</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">Mapper层的核心职责是什么？它不应该处理哪些事务？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">Mapper层的核心职责是执行SQL操作（增删改查）和处理基础数据转换。它不应该做的事情包括：处理业务逻辑、进行数据校验和处理事务。</div>
          </div>
          <div class="card-source">来源: 3.1 Mapper层：专注数据的"手艺人"</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">Service层的核心职责是什么？请根据文档列举。</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">Service层的核心职责包括：组合多个Mapper操作、实现业务规则、处理事务以及进行异常处理。</div>
          </div>
          <div class="card-source">来源: 3.2 Service层：业务逻辑的"指挥官"</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">模式</div>
          <div class="card-question">在接口设计上，Mapper层和Service层应遵循的“单一职责”原则有何不同？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">模式</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">Mapper层的单一职责原则是每个方法对应一个SQL操作（原子操作），而Service层的单一职责原则是每个方法对应一个完整的业务用例（组合操作）。</div>
          </div>
          <div class="card-source">来源: 6.1 接口设计原则</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">最佳实践</div>
          <div class="card-question">关于事务管理，文档强调的正确实践是什么？将@Transactional注解放在Mapper层是正确的吗？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">最佳实践</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">正确的实践是将事务注解（@Transactional）加在Service层的方法上，以确保多个数据库操作能够在一个事务中完成。文档指出，将该注解放在Mapper层接口的方法上是一种常见错误。</div>
          </div>
          <div class="card-source">来源: 6.2 事务管理</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">最佳实践</div>
          <div class="card-question">文档推荐在哪一层统一进行异常处理，例如将数据库查询结果为空的情况转换为自定义的异常？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">最佳实践</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">文档推荐在Service层统一处理异常。示例代码展示了在Service层捕获Mapper返回的空结果，并抛出自定义的`UserNotFoundException`。</div>
          </div>
          <div class="card-source">来源: 6.3 异常处理</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">常见错误</div>
          <div class="card-question">文档中指出的一个常见错误是将业务逻辑（如数据过滤）写在Mapper层。正确的做法应该是什么？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">常见错误</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">正确的做法是将业务逻辑（例如根据状态过滤数据）放在Service层进行处理，保持Mapper层只负责纯粹的数据库操作。</div>
          </div>
          <div class="card-source">来源: 5.1 把业务逻辑写在Mapper层？</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">在对Service层进行单元测试时，文档推荐采用什么策略来处理其依赖的Mapper层？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">文档推荐的策略是Mock Mapper层。通过模拟Mapper的行为，可以使Service层的单元测试专注于验证业务逻辑的正确性，而无需依赖真实的数据库。</div>
          </div>
          <div class="card-source">来源: 7.2 单元测试策略</div>
        </div>
      </div>
    </div>

      </div>
    </body>
    </html>
